package com.study.javaBasic.queue;

/**
 * @ClassName: CircleArrayQueue
 * @Description:
 * @author: 王胜
 * @date 2021/6/18
 * @Version: 1.0
 **/
public class CircleArrayQueue{
    /**
     * 队首的下标
     */
    private int front=0;
    /**
     * 队尾的下标
     */
    private int rear=0;
    /**
     * 队列的最大长度
     */
    private int maxsize;
    /**
     * 实际的队列
     * 比最大的长度大1
     * 约定队尾和队首之间空一个
     */
    private int[] queue;
    public CircleArrayQueue(int maxsize){
        this.maxsize=maxsize;
        queue=new int[maxsize+1];
    }

    /**
     * 队列是否空
     * @return
     */
    public Boolean isEmpty(){
        return front==rear;
    }

    /**
     * 队列是否满
     * @return
     */
    public Boolean isFull(){
        return (rear+1+maxsize+1)%(maxsize+1)==front;
    }

    /**
     * 加入一个队列元素
     * @param i
     */
    public void addQueue(int i){
        if(isFull()){
            System.out.println("队列已满--------");
            return;
        }
        queue[(rear++)%(maxsize+1)]=i;
    }

    public Integer getQueue(){
        if(isEmpty()){
            System.out.println("队列已空------------");
            return null;
        }
        return queue[(front++)%(maxsize+1)];
    }
    public Integer size(){
        return (rear+(maxsize+1)-front)%(maxsize+1);
    }
    public void showQueue(){
        int count=0;
        for (int i = front; i < front+size(); i++) {
            System.out.printf("queue[%d]=%d\n",count++,queue[i%(maxsize+1)]);
        }
    }
    public Integer showFront(){
        if(isEmpty()){
            System.out.println("队列已空------------");
            return null;
        }
        return queue[front];
    }
}